﻿using System;
using System.Collections.Generic;
using System.Reflection;
using xycn.IDbService;

namespace xycn.pluginsupport.BaseHelperPlugins
{
    /// <summary>
    /// 数据ORM
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class DbHelper
    {
        /// <summary>
        /// 保存ORM插件对象工厂实例
        /// </summary>
        static Dictionary<string, IPluginFactory.IPluginFactory> ORMInterfaces = 
            new Dictionary<string, IPluginFactory.IPluginFactory>();

        /// <summary>
        /// 开放类工厂，供controller调用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IDbService.IDbService CreateDbService(string name)
        {
            if (!ORMInterfaces.ContainsKey(name))
            {
                return null;
            }

            IDbService.IDbService ret = null;
            IPluginFactory.IPluginFactory serviceFactory = ORMInterfaces[name];
            if (serviceFactory != null)
            {
                ret = (IDbService.IDbService)serviceFactory.CreatePlugin();
            }

            return ret;
        }

        /// <summary>
        /// 添加ORM插件接口
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dbfactory"></param>
        /// <returns></returns>
        public static void Init(string name, IPluginFactory.IPluginFactory dbfactory)
        {
            if (ORMInterfaces.ContainsKey(name))
            {
                return;
            }

            ORMInterfaces.Add(name, dbfactory);
            return;
        }
    }
}
